package com.kai.module_cartoon.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import com.chad.library.adapter.base.BaseBinderAdapter
import com.kai.base.base.recycler.decorate.HorizontalItemSpaceDecoration
import com.kai.base.utils.AppUtils
import com.kai.base.utils.IStringUtils
import com.kai.base.utils.LayoutManagerUtil
import com.kai.base.utils.ViewUtils
import com.kai.module_cartoon.R



/**
 * @Author: 小强同学
 * @Date:  2021/3/23 9:56
 * @Description:  java类作用描述
 */
class HorizontalListView : LinearLayout {

    private var mBinding: View? = null
    private var chlHeadIm: ImageView? = null
    private var chlHeadTv: TextView? = null
    private var chlRyView1: RecyclerView? = null

    // 可以直接快速使用，也可以继承BaseBinderAdapter类，重写自己的相关方法
    private var mAdapter = BaseBinderAdapter()

    constructor(context: Context) : super(context) {
        mBinding = View.inflate(context, R.layout.cartoon_horizontal_list, this)
        initView(null)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        mBinding = View.inflate(context, R.layout.cartoon_horizontal_list, this)
        initView(attrs)
    }

    private fun initView(attrs: AttributeSet?) {
        chlHeadIm=mBinding?.findViewById(R.id.chl_headIm)
        chlHeadTv=mBinding?.findViewById(R.id.chl_headTv)
        chlRyView1=mBinding?.findViewById(R.id.chl_ryView1)
        if (attrs != null) {
            val typedArray =
                context.obtainStyledAttributes(attrs, R.styleable.cartoon_HorizontalListView)
            typedArray?.apply {
                val a = typedArray.getResourceId(
                    R.styleable.cartoon_HorizontalListView_cartoon_image,
                    0
                )
                val b = typedArray.getString(R.styleable.cartoon_HorizontalListView_cartoon_textMsg)
                val c =
                    typedArray.getColor(R.styleable.cartoon_HorizontalListView_cartoon_textColor, 0)
                if (a != 0) {
                     chlHeadIm?.setImageResource(a)
                }
                if (IStringUtils.isNoEmpty(b)) {
                    chlHeadTv?.text = b
                }
                if (c != 0) {
                    chlHeadTv?.setTextColor( c)
                }
            }
        }
        mAdapter?.addItemBinder(HorizonBean::class.java, HorizontalAdapter())
        //设置横向排列
        chlRyView1?.apply {
            overScrollMode = View.OVER_SCROLL_NEVER //去除涟漪下拉效果
            layoutManager = LayoutManagerUtil.getHorizontalLinearLayoutManager(context)
            adapter = mAdapter
            addItemDecoration(
                HorizontalItemSpaceDecoration(
                    ViewUtils.dp2px(2),
                    ViewUtils.dp2px(15),
                    ViewUtils.dp2px(10)
                )
            )
        }


    }

    fun setUpdate(list: MutableList<HorizonBean>) {
        chlHeadIm?.visibility=View.VISIBLE
        chlHeadTv?.visibility=View.VISIBLE
        mAdapter?.setList(list)
    }


}